<?php
namespace app\index\controller;
use app\index\model\Index_model;
use think\Cookie;
use think\Db;

class Index extends Common {
	// 首页
	public function index() {
		// dump($_SESSION['think']['username']['level']);die;
		$uid = $_SESSION['think']['username']['0']['user_id'];
		// dump($uid);die;
		$aaa = $this->getOrgId($uid);
		// dump($aaa);die;
		$rev = new Index_model;
		$res = $rev->getindex();
		$aaa = Cookie::get('username');
		// dump($res['mail']);die;
		return view('index@Index/index', [
			'node' => $res['node'],
			'amount' => $res['amount'],
			'mail' => $res['mail'],
			'id' => $res['id'],
			'file_num' => $res['file_num'],
			'borrow_num' => $res['borrow_num'],
			'user_num' => $res['user_num'],
		]);
	}

	public function getOrgId($id) {
		// dump($id);die;
		$res = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_organization_relation cuor', 'cuor.user_id = cu.user_id')
			->join('cn_organization co', 'co.org_id = cuor.org_id')
			->where('cu.user_id', $id)
			->where('cu.level', '1')
			->field('co.org_id')
			->find();
		if ($res) {
			$res[0]['org_id'] = $res['org_id'];
			$ares = Db::table('cn_organization')
				->where('path', 'like', '%' . ',' . $res['org_id'] . ',')
				->field('org_id')
				->select();
			$data = array_merge($res, $ares);
			unset($data['org_id']);
			foreach ($data as $key => $value) {
				$data[$key] = $value['org_id'];
			}
			$data = implode(',', $data);
			return $data;
		} else {
			$rres = Db::table('cn_user')
				->alias('cu')
				->join('cn_user_organization_relation cuor', 'cuor.user_id = cu.user_id')
				->join('cn_organization co', 'co.org_id = cuor.org_id')
				->where('cu.user_id', $id)
				->where('cu.level', '0')
				->field('co.org_id')
				->find();
			// dump($rres);die;
			return $rres['org_id'];
		}

	}

}
